import './App.css';
import './style';

import React from 'react';
import { createRoot } from 'react-dom/client';
import { ErrorBoundary } from 'react-error-boundary';
import { Provider } from 'react-redux';

import App from './App.tsx';
import ErrorPage from './components/ErrorBoundary.tsx';
import { globalConfig } from './config.ts';
import { setupI18n } from './locales/index.ts';

import { store } from './store/index.ts';
import { themeSettings } from './theme/settings.ts';
import { localStg } from './utils/storage.ts';
import { setupAppVersionNotification, setupNProgress, setupIconifyOffline, setupDayjs } from './plugins';

function initGlobalConfig() {
  /** - 初始化默认主题颜色 */
  globalConfig.defaultThemeColor = localStg.get('themeColor') || themeSettings.themeColor;

  /** - 初始化默认语言 */
  globalConfig.defaultLang = localStg.get('lang') || globalConfig.defaultLang;

  /** - 初始化默认暗色模式 */
  globalConfig.defaultDarkMode = localStg.get('darkMode') || globalConfig.defaultDarkMode;
}

function setUpApp() {
  initGlobalConfig();
  setupI18n();

  const rootEl = document.getElementById('root');
  if (rootEl) {
    const root = createRoot(rootEl);
    root.render(
      <React.StrictMode>
        <ErrorBoundary fallbackRender={ErrorPage}>
          <Provider store={store}>
            <App />
          </Provider>
        </ErrorBoundary>
      </React.StrictMode>,
    );
  }

  setupNProgress();
  setupIconifyOffline();
  setupDayjs();
  setupAppVersionNotification();
}

setUpApp();
